package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 新增分类
     * @param dto
     * @return
     */
    @Override
    public Result addCategory(CategoryDTO dto) {
        //判断是否为空
        if (dto.getName() == null || dto.getSort() == null) {
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //判断类名是否已经存在
        if (categoryMapper.selectByName(dto.getName()) != null){
            return Result.error(MessageConstant.USERNAME_EXIST);
        }

        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        category.setStatus(StatusConstant.ENABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.addCategory(category);
        return Result.success();
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public Result pageQuery(CategoryPageQueryDTO dto) {
        //判断是否为空，二如果为空则设置默认页数
        if (dto.getPage() == null){
            dto.setPage(1);
        }
        if (dto.getPageSize() == null){
            dto.setPageSize(5);
        }
        //调用分页工具进行分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //调用mapper方法
        Page<Category> categories = categoryMapper.pageQuery(dto);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(categories.getTotal());
        pageResult.setRecords(categories.getResult());
        return Result.success(pageResult);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Long id) {
        //判断id是否为空
        if (id == null){
            return Result.error(MessageConstant.ID_NOT_EXIST);
        }
        categoryMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 修改类型
     * @param dto
     * @return
     */
    @Override
    public Result updateType(CategoryDTO dto) {
        if (dto.getId() == null){
            return Result.error(MessageConstant.ID_NOT_EXIST);
        }
        Category category = new Category() ;
        BeanUtils.copyProperties(dto,category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
        return Result.success();
    }

    /**
     * 修改分类禁止启用状态
     * @param status
     * @return
     */
    @Override
    @AutoFill(OperationType.UPDATE)
    public Result updateStatus(Integer status , Long id) {
        if (status == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);

        return Result.success();
    }

    /**
     * 根据类型分类查询
     * @param type
     * @return
     */
    @Override
    public Result selectByList(Integer type) {
        //判断是否为空

        //调用方法查询分类
        List<Category> list = categoryMapper.list(type);
        return Result.success(list);
    }
}
